<template>
<el-dialog :visible.sync="visible" :title="title" v-if="visible" @close="closeDialog">
  <el-form :model="formData" label-position="right" label-width="80px" :rules="rules" ref="formData">
    <el-form-item label="广告名称" prop="name">
      <el-input placeholder="请输入广告名称"  v-model="formData.name"/>
    </el-form-item>
    <el-form-item label="广告链接">
      <el-input placeholder="请输入广告链接"  v-model="formData.href"/>
    </el-form-item>
    <el-form-item label="排序值" prop="orderNo">
      <el-input-number placeholder="请输入排序值" :min="0" v-model="formData.orderNo"/>
    </el-form-item>
    <el-form-item label="打开方式">
      <el-select v-model="formData.target">
        <el-option value="_self" label="本页面"/>
        <el-option value="_blank" label="新页面"/>
      </el-select>
    </el-form-item>
    <el-form-item label="开始时间" prop="expiresStart">
      <el-date-picker v-model="formData.expiresStart" type="datetime" placeholder="选择开始时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="结束时间" prop="expiresEnd">
      <el-date-picker v-model="formData.expiresEnd" type="datetime" placeholder="选择结束时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="广告图片" prop="src">
      <el-upload :action="uploadUrl" :headers="uploadHeader" :file-list="fileList" list-type="picture-card" :limit="1" ref="uploadImage" :on-success="uploadSuccess" :on-exceed="onExceed" :before-upload="beforeUpload">
        <i class="el-icon-plus"></i>
        <div slot="file" slot-scope="{file}">
          <img :src="file.url" alt="" class="el-upload-list__item-thumbnail"/>
          <div class="el-upload-list__item-actions">
            <span @click="showImageDialog(file)">
              <i class="el-icon-zoom-in"></i>
            </span>
            <span @click="removeImage(file)">
              <i class="el-icon-delete"></i>
            </span>
          </div>
        </div>
      </el-upload>
    </el-form-item>
    <el-form-item label="状态">
      <el-radio-group v-model="formData.status">
        <el-radio :label="1">可用</el-radio>
        <el-radio :label="2">禁用</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="update">提交</el-button>
      <el-button type="warning" @click="visible = false">取消</el-button>
    </el-form-item>
  </el-form>
  <el-dialog append-to-body title="查看图片" :visible.sync="showImage" v-if="showImage">
    <div style="height: 100%;width: 100%;display: flex;align-items: center;justify-content: center;">
      <img :src="showImageSrc" alt="" style="display: block;max-height: 100%;max-width: 100%">
    </div>
  </el-dialog>
</el-dialog>
</template>

<script>
const formData = {
  id: '',
  name: '',
  orderNo: '',
  target: '_self',
  expiresStart: '',
  expiresEnd: '',
  src: '',
  href: '',
  status: 1
}
export default {
  name: 'AdUpdate',
  data () {
    return {
      visible: false,
      title: '添加数据',
      formData: {
        ...formData
      },
      rules: {
        name: [{
          required: true,
          message: '请输入广告名',
          trigger: 'blur'
        }],
        orderNo: [{
          required: true,
          message: '请输入排序值',
          trigger: 'blur'
        }],
        expiresStart: [{
          required: true,
          message: '请选择开始日期和时间',
          trigger: 'blur'
        }],
        expiresEnd: [{
          required: true,
          message: '请选择结束日期和时间',
          trigger: 'blur'
        }],
        src: [{
          required: true,
          message: '请上传广告图片'
        }]
      },
      uploadUrl: process.env.VUE_APP_BASE_URL + 'upload',
      uploadHeader: {
        token: ''
      },
      fileList: [],
      showImage: false,
      showImageSrc: ''
    }
  },
  methods: {
    init (id) {
      this.visible = true
      this.formData = { ...formData }
      if (id) {
        this.title = '修改数据'
        this.$http.get(`ad/info/${id}`).then(({ data }) => {
          this.formData = { ...data }
          this.fileList = [{ url: `${process.env.VUE_APP_BASE_URL}img/${data.src}` }]
        })
      }
    },
    update () {
      this.$refs.formData.validate(valid => {
        if (valid) {
          this.$http.post('ad/update', this.formData).then(() => {
            this.$alert(`${this.formData.id ? '修改' : '添加'}成功`, { type: 'success' }).then(() => {
              this.visible = false
              this.$emit('onCloseDialog')
            })
          })
        }
      })
    },
    uploadSuccess (res) {
      if (res.code === 200) {
        this.formData.src = res.data
      } else {
        this.$message.error(res.msg)
      }
    },
    onExceed () {
      this.$message.error('请先删除原图片')
    },
    beforeUpload (file) {
      const isImage = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif'
      if (!isImage) {
        this.$message.error('请选择正确的格式！')
      }
      const isLt20M = file.size / 1024 / 1024 < 20
      if (!isLt20M) {
        this.$message.error('文件大小不能超过20M！')
      }

      return isImage && isLt20M
    },
    closeDialog () {
      this.fileList.length = 0
      this.formData = { ...formData }
    },
    showImageDialog (file) {
      this.showImage = true
      this.showImageSrc = file.url
    },
    removeImage (file) {
      this.$refs.uploadImage.handleRemove(file)
      this.formData.src = ''
    }
  },
  created () {
    this.$nextTick(() => {
      this.uploadHeader.token = this.$cookies.get('token')
    })
  }
}
</script>

<style scoped>

</style>
